package com.xinqi.modules.exam.analyze.enmus;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import com.xinqi.common.base.enums.BaseEnum;
import com.xinqi.modules.exam.analyze.dto.GradeDistributionDTO;
import lombok.Getter;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 测评成绩计算规则区间要替换一下，换成：
 * <p>
 * 总分90%<成绩=<总分100%、 A
 * <p>
 * 总分80%<成绩=<总分90%、 B
 * <p>
 * 总分70%<成绩=<总分80%、 C
 * <p>
 * 总分60%<成绩=<总分70%、 D
 * <p>
 * 成绩<总分60%、 E
 *
 * @author linkai
 * @date 2023/7/16
 */
public enum ScoreRangeEnum implements BaseEnum {
    A级("A级", "总分80%<成绩=<总分100%"),
    B级("B级", "总分60%<成绩=<总分80%"),
    C级("C级", "总分40%<成绩=<总分60%"),
    D级("D级", "总分20%<成绩=<总分40%"),
    E级("E级", "成绩<总分20%"),
    ;

    @Getter(onMethod_ = {@JsonValue})
    private final String code;

    @Getter
    private final String describe;

    ScoreRangeEnum(String code, String describe) {
        this.code = code;
        this.describe = describe;
    }

    @JsonCreator
    public static ScoreRangeEnum of(String code) {
        return BaseEnum.of(ScoreRangeEnum.class, code);
    }

    /**
     * 计算当前分数属于什么级别
     */
    public static ScoreRangeEnum scoreRange(BigDecimal score, BigDecimal totalScore) {
        return scoreRange(score.doubleValue(), totalScore.doubleValue());
    }

    /**
     * 计算当前分数属于什么级别
     */
    public static ScoreRangeEnum scoreRange(double score, double totalScore) {
        double percent = score / totalScore;
        if (percent >= 0.9) {
            return A级;
        } else if (percent >= 0.8) {
            return B级;
        } else if (percent >= 0.7) {
            return C级;
        } else if (percent >= 0.6) {
            return D级;
        } else {
            return E级;
        }
    }

    /**
     * 计算当前分数属于什么级别
     */
    public static List<GradeDistributionDTO> initGradeDistribution() {
        List<GradeDistributionDTO> list = new ArrayList<>(ScoreRangeEnum.values().length);
        for (ScoreRangeEnum scoreRange : ScoreRangeEnum.values()) {
            GradeDistributionDTO e = new GradeDistributionDTO();
            e.setScoreRange(scoreRange);
            e.setStudentNum(0);
            list.add(e);
        }
        return list;
    }
}
